﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;
using ForzaGT3.Areas.Admin.Models;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class PageController : Controller
    {
        //
        // GET: /Admin/Page/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Url", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Title", Request.QueryString, whereValues);

           

            // Get data from database
            int totalRecords;
            IQueryable<Content> data = Data.Content.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "Page";

            return View(data.ToList());
        }

        [HttpGet]
        public virtual ActionResult Edit(Guid Id)
        {
            Data.Content data = Data.Content.GetById(Id);
            if (data != null)
            {
                PageModel model = new PageModel();
                model.Id = data.ContentId;
                model.Url = data.Url;
                model.Title = data.Title;
                model.Intro = data.Intro;
                model.Content = data.Body;

                model.TitleEnglish = data.TitleEng;
                model.IntroEnglish = data.IntroEng;
                model.ContentEnglish = data.BodyEng;

                ViewData.Model = model;
            }
            return View();
        }

        [HttpPost]
        public virtual ActionResult Edit(PageModel model)
        {
            if (ModelState.IsValid)
            {
                Data.Content data = Data.Content.GetById(model.Id);
                if (data != null)
                {
                    data.Url = model.Url;
                    data.Title = model.Title;
                    data.Intro = model.Intro;
                    data.Body = model.Content;

                    data.TitleEng = model.TitleEnglish;
                    data.IntroEng = model.IntroEnglish;
                    data.BodyEng = model.ContentEnglish;

                    data.ModifiedDate = DateTime.Now;
                    data.ModifiedBy = HttpContext.User.Identity.Name;
                    data.Update();

                    return RedirectToAction(MVC.Admin.Page.Index());
                }
            }

            return View(model);
        }
    }
}
